<?php

namespace app\modules\admingoods\controllers;

use Yii;
use app\components\AdminController;
use yii\data\Pagination;
use common\models\Tools;
use common\models\Goods;
use common\models\GoodsAttr;
use common\models\CateAttrVal;
use common\models\GoodsImg;
use common\models\GoodsPackage;
use common\models\CateAttr;

/**
 * Default controller for the `admingoods` module
 */
class PublishController extends AdminController
{
    public $enableCsrfValidation = false;
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        $userid = Yii::$app->user->identity->id;
        $goodsType = Tools::GetSafeParam('goodstype', 1, 0);
        if($goodsType != 1 && $goodsType != 7){
            Tools::get_show_msg('/user', '错误的商品类型', 0, 3);
        }
        $role_id = \common\models\SysUser2role::getUserRoleId($userid);
        if($role_id > 0 && $role_id<=3){
            //检测是否选择了区域 
            if(Yii::$app->user->identity->region_1==0 || Yii::$app->user->identity->region_2==0 || Yii::$app->user->identity->region_3==0){
                Tools::get_show_msg('/', '区域定位出错！', 0, 3);
            }
            $category = \common\models\Category::getSubCate($goodsType);
        }
        
        if(empty($category)){
            Tools::get_show_msg('/', '没有对应的商品分类！', 0, 3);
        }
        $title = "";
        if($goodsType==1){
            $title = "广告位发布";
        }
        if($goodsType==7){
            $title = "广告设备发布";
        }
        
        $this->getView()->title = $title;
        return $this->render('index',['category'=>$category,'first_cate'=>$goodsType]);
    }
    
    public function actionUpdate(){
        $goods_id = Tools::GetSafeParam('goods_id', 0, 0);
        $userid = Yii::$app->user->identity->id;
        $region_3 = Yii::$app->user->identity->region_3;
        
        $goods = Goods::getGoodsByGoodsid($goods_id);
        //权限检测
        $roleid = \common\models\SysUser2role::getUserRoleId($userid);
        switch ($roleid){
            case 1://管理员，查看所有商品
                break;
            case 2://区域代理商，查看所属区域的商品
                if($goods['region_3'] != $region_3){
                    Tools::get_show_msg('/admingoods/default/index', '该商品不属于您代理的区域！', 0, 4);
                }
                break;
            case 3://商家，查看自己发布的商品
                if($goods['seller_id'] != $userid){
                    Tools::get_show_msg('/admingoods/default/index', '不能操作不属于您的商品！', 0, 4);
                }
                
                break;
            default:
        }
        
        if($goods['status'] == 1){
            Tools::get_show_msg('/admingoods/default/index', '请先下架商品再编辑！', 0, 4);
        }
        
        $goods['order_endtime'] = date('Y-m-d',$goods['order_endtime']);
        
        $attr_input = CateAttr::getAttrlistByCate($goods['third_cate']);
//        echo "<pre>"; print_r($attr_input);exit;
        foreach ($attr_input as $key=>$value){
            if($value['type']==0){
                $goods_attr = GoodsAttr::getAttrByGoodsnoAndAttrid($goods['goods_no'],$value['id']);
                $attr_input[$key]['val'] = $goods_attr['val'];
            }
            if($value['type']==1){
                $goods_attr = GoodsAttr::getAttrByGoodsnoAndAttrid($goods['goods_no'],$value['id']);
                
                $option = CateAttrVal::getCateAttrValByAttrId($value['id']);
                foreach($option as $op_key=>$op_value){
                    $option[$op_key]['selected'] = $goods_attr['attr_val'] == $op_value['id'] ? 1 : 0;
                }
                $attr_input[$key]['option'] = $option;
            }
            if($value['type']==2){
                $goods_attr = GoodsAttr::getAttrByGoodsnoAndAttrid($goods['goods_no'],$value['id']);
                $option = CateAttrVal::getCateAttrValByAttrId($value['id']);
                
                $checked_arr = explode(',', $goods_attr['attr_val']);
                
                foreach($option as $op_key=>$op_value){
                    $option[$op_key]['checked'] = in_array($op_value['id'], $checked_arr) ? 1 : 0;
                }
                $attr_input[$key]['option'] = $option;
            }
            
        }
        $package = '';
        if($goods['have_package'] == 1){//广告位商品不允许修改套餐内容
            $package = GoodsPackage::getAllPackageByGoodsno($goods['goods_no'],$goods['first_cate']);
        }
        
        $goods['imgs'] = GoodsImg::getImgsByGoodsno($goods['goods_no']);
        
        $introduction = \common\models\GoodsIntroduction::getIntroduction($goods['goods_no']);
        $goods['introduction'] = htmlspecialchars_decode($introduction);
        
        
        $data['goods'] = $goods;
        $data['attr_input'] = $attr_input;
        $data['package'] = $package;
        
        //echo "<pre>"; print_r($data);exit;
        
        $this->getView()->title = "商品编辑";
        return $this->render('update',['data'=>$data]);
    }
}
